package com.wc.alorithm_blue_bridge_history.littlewhite.Q231223.Q10006;

import java.io.*;
import java.util.StringTokenizer;

/**
 * @Author congege
 * @Description https://www.lanqiao.cn/problems/10006/learning/?contest_id=154
 * @Date Created in 2023/12/23-19:23
 */
public class Main {
    static int maxN = 2 * (int) 1e5;
    static boolean[] isPrime = new boolean[maxN + 10];
    static int[] primes = new int[maxN + 10];
    static int idx = 0;
    static int[] array = new int[maxN];
    static FastReader in = new FastReader();
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) {
        ola(maxN);
        int n = in.nextInt();
        long ans = 0;
        for (int i = 1; i <= n; i++) {
            int num = in.nextInt();
            int idx = halfSearch(num);
            if (idx == 1) {
                ans += primes[idx] - num;
            } else {
                ans += Math.min(primes[idx] - num, num - primes[idx - 1]);
            }
        }
        out.println(ans);
        out.flush();
    }

    public static int halfSearch(int val) {
        int l = 1;
        int r = 10000;
        int mid;
        while (l <= r) {
            mid = l + (r - l) / 2;
            if (primes[mid] >= val) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return l;
    }

    static public void ola(int num) {
        isPrime[1] = true;
        for (int i = 2; i <= num; i++) {
            if (!isPrime[i]) {
                primes[++idx] = i;
            }
            for (int j = 1; i * primes[j] <= num; j++) {
                isPrime[primes[j] * i] = true;
                if (i % primes[j] == 0) {
                    break;
                }
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    int nextInt() {
        return Integer.parseInt(next());
    }
}

